import React, {Component} from 'react'
import PropTypes from 'prop-types'

export default class Counter extends Component {
    static propTypes = {
        count: PropTypes.number.isRequired,
        increment: PropTypes.func.isRequired,
        decrement: PropTypes.func.isRequired,
        incrementAsync: PropTypes.func.isRequired,
    };
    increment = () => {
        const num = this.select.value * 1;
        this.props.increment(num);
    };

    decrement = () => {
        const num = this.select.value * 1;
        this.props.decrement(num);
    };

    incrementIfOdd = () => {
        const num = this.select.value * 1;
        const {count} = this.props;
        if (count % 2 === 1) {
            this.props.increment(num);
        }
    };

    incrementAsync = () => {
        const num = this.select.value * 1;
        this.props.incrementAsync(num);
    };

    render() {
        const {count} = this.props;
        return (
            <div>
                <p>click {count} times</p>
                <div>
                    <select ref={select => this.select = select}>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    &nbsp;&nbsp;
                    <button onClick={this.increment}>&nbsp;+&nbsp;</button>
                    <button onClick={this.decrement}>&nbsp;-&nbsp;</button>
                    <button onClick={this.incrementIfOdd}>increment if odd</button>
                    <button onClick={this.incrementAsync}>increment async</button>
                </div>
            </div>
        )
    }
}
